package com.example.xcService.pojo.channels.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.xcService.pojo.channels.ChannelResult;
import com.example.xcService.pojo.channels.ChannelService;
import com.example.xcService.pojo.tool.HttpTool;
import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/*
* 娱尚
* */
@Slf4j
public class Plugin_ECARD_P037 implements ChannelService {


    @Override
    public ChannelResult QueryBalance(JSONObject apiConfig) {
        System.out.println("成功调用了QueryBalance");
        return  ChannelResult.success("success");
    }

    @Override
    public ChannelResult Submit(JSONObject apiConfig, Map<String, Object> data) {

        try {
            String AppKey = apiConfig.getString("AppKey");
            String BuyCount = "1";
            String ChargeAccount = String.valueOf(data.get("AccountVal"));
            String CustomerIP = "120.26.223.111";
            String MOrderID = String.valueOf(data.get("ChannelOrderCode"));
            String ProductCode = String.valueOf(data.get("SUPProductId"));
            long unixTimestamp = System.currentTimeMillis();
            String TimesTamp = String.valueOf(unixTimestamp);
            String AppSecret = apiConfig.getString("AppSecret");
            String submitUrl = apiConfig.getString("SubmitUrl");

            StringBuilder append = new StringBuilder().append(AppKey)
                    .append(BuyCount).append(ChargeAccount)
                    .append(CustomerIP).append(MOrderID)
                    .append(ProductCode).append(TimesTamp).append(AppSecret);
            String sign = md5Encrypt(append.toString());

            HashMap<String, String> submitParams = new HashMap<>();
            submitParams.put("AppKey",AppKey);
            submitParams.put("TimesTamp",TimesTamp);
            submitParams.put("ProductCode",ProductCode);
            submitParams.put("BuyCount",BuyCount);
            submitParams.put("MOrderID",MOrderID);
            submitParams.put("ChargeAccount",ChargeAccount);
            submitParams.put("CustomerIP",CustomerIP);
            submitParams.put("Sign",sign);

            String respStr = HttpTool.HttpPost(submitUrl, submitParams);
            JSONObject respObj = JSONObject.parseObject(respStr);
            String code = respObj.getString("Code");
            String msg = respObj.getString("Msg");
            if (code.equals("999")||code.equals("998") ||code.equals("997") || code.equals("9")){
                return ChannelResult.IN_PROCESS("充值中");
            }
            if (code.equals("0")){
                return ChannelResult.error(msg);
            }
            return ChannelResult.FAILED(msg);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("娱尚提单接口错误：",e);
            return  ChannelResult.error("娱尚提单接口错误");
        }

    }

    @Override
    public ChannelResult Query(JSONObject apiConfig, Map<String, Object> data) {
        try {
            String AppKey = apiConfig.getString("AppKey");
            long unixTimestamp = System.currentTimeMillis();
            String TimesTamp = String.valueOf(unixTimestamp);
            String MOrderID = String.valueOf(data.get("ChannelOrderCode"));
            String AppSecret = apiConfig.getString("AppSecret");
            String queryUrl = apiConfig.getString("QueryUrl");
            StringBuilder append = new StringBuilder().append(AppKey)
                    .append(TimesTamp).append(MOrderID).append(AppSecret);

            String sign = md5Encrypt(append.toString());

            HashMap<String, String> queryParams = new HashMap<>();
            queryParams.put("AppKey",AppKey);
            queryParams.put("TimesTamp",TimesTamp);
            queryParams.put("Sign",sign);
            queryParams.put("MOrderID",MOrderID);

            String respStr = HttpTool.HttpPost(queryUrl, queryParams);
            JSONObject respObj = JSONObject.parseObject(respStr);
            String code = respObj.getString("Code");
            String msg = respObj.getString("Msg");
            if (code.equals("999")){
                String respData = respObj.getString("Data");
                JSONObject dataObj = JSONObject.parseObject(respData);
                String orderState = dataObj.getString("OrderState");
                if (orderState.equals("2")){
                    return ChannelResult.success("充值成功");
                }
                if (orderState.equals("1")){
                    return ChannelResult.IN_PROCESS("充值中");
                }
                if (orderState.equals("3")){
                    return ChannelResult.FAILED(msg);
                }
            }
            return ChannelResult.error(msg);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("娱尚查询接口错误：",e);
            return  ChannelResult.error("娱尚查询接口错误");
        }

    }


    @Override
    public ChannelResult Notify(JSONObject apiConfig, Map<String, Object> data) {
        System.out.println("成功调用了Notify");
        return  ChannelResult.success("success");
    }

    public static String md5Encrypt(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                hexString.append(String.format("%02X", b));
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }


}
